<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/17
 * Time: 下午2:45
 */
class Taskworker extends CI_Controller
{
    /**
     * 发布任务
     */
    public function publish()
    {
        Permission::zy();
        $data = array('header_json' => $this->lyw_common_model->buildHeaderJson());
        $this->load->view('taskwork_publish', $data);
    }

    public function doPublish()
    {
        Permission::zy(1);
        $text = isset($_POST['text']) ? $_POST['text'] : '';
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        $title = isset($_POST['title']) ? $_POST['title'] : '';
        $url = isset($_POST['url']) ? $_POST['url'] : '';
        $this->load->model('Lyw_original_article_model');
        sessionOpen();
        if ($this->Lyw_original_article_model->add($_SESSION['user_id'], $title, $text, $html, $url)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('failure'));
        }
    }

    public function task()
    {
        Permission::zy();
        $user_id = $_SESSION['user_id'];
        $gonggao = '公告:' . file_get_contents(BASEPATH . '../assets/gonggao.txt');
        $data = array(
            'user_id' => $user_id,
            'user_type' => $_SESSION['user_type'],
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
            'gonggao' => $gonggao
        );
        $this->load->view('admin_task', $data);
    }

    public function myTask()
    {
        Permission::zy();
        $user_id = $_SESSION['user_id'];
        $data = array(
            'user_id' => $user_id,
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
        );
        $this->load->view('taskworker_my_task', $data);
    }

    /**
     * 任务专员的列表是根据原文发布者的ID来拉取
     */
    public function getMyTaskJson()
    {
        Permission::zy(1);
        $page = isset($_POST['page']) ? $_POST['page'] : 0;
        $rows = isset($_POST['rows']) ? $_POST['rows'] : 0;
        $this->load->model('Lyw_original_article_model');
        $id_arr = $this->Lyw_original_article_model->getIdListByUserid($_SESSION['user_id']);
        if (empty($id_arr)) {
            exit(json_encode(array('list' => array(), 'count_all' => 0)));
        }
        $id_str = arrToStrById($id_arr);
        $this->load->model('Lyw_article_manager_model');
        $manager_list = $this->Lyw_article_manager_model->getTaskWorkerOurselfTask($id_str, $page, $rows);
        $this->lyw_common_model->addInfoToManagerList($manager_list);
        $this->Lyw_article_manager_model->getFinishListInfo($manager_list);
        $countAll = $this->Lyw_article_manager_model->getTaskWorkerOurselfTaskCount($id_str);
        exit(json_encode(array('list' => $manager_list, 'count_all' => $countAll)));
    }

    /**
     * 校对任务
     */
    public function proofread()
    {
        Permission::zy();
        $this->load->view('taskworker_proofead', $this->lyw_common_model->readProofread());
    }

    public function proofreadSave()
    {
        Permission::zy(1);
        $text = isset($_POST['text']) ? $_POST['text'] : '';
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        $title = isset($_POST['title']) ? $_POST['title'] : '';
        $manager_id = isset($_POST['manager_id']) ? $_POST['manager_id'] : 0;
        $proofread_id = isset($_POST['proofread_id']) ? $_POST['proofread_id'] : 0;
        $is_received = isset($_POST['is_received']) ? $_POST['is_received'] : 0;
        $original_user_id = isset($_POST['original_user_id']) ? $_POST['original_user_id'] : 0;
        $translation_user_id = isset($_POST['translation_user_id']) ? $_POST['translation_user_id'] : 0;
        $status = isset($_POST['status']) ? $_POST['status'] : '';
        $url = isset($_POST['url']) ? $_POST['url'] : '';
        $score = isset($_POST['score']) ? $_POST['score'] : 0;
        if ($score > 5) {
            exit('wrong score ! Is greater than 5');
        }
        if (empty($text) || empty($manager_id) || empty($translation_user_id)) {
            exit('empty');
        }
        $this->load->model('Lyw_proofread_article_model');
        sessionOpen();
//        if($manager_id == 120){
//            exit('illage');
//        }
        $this->load->model('Lyw_article_manager_model');
        $manager_info = $this->Lyw_article_manager_model->getOneListInfoById($manager_id);
        if($manager_info['is_proofreaded'] == 2 && $status == 1){
            exit('error : proofreaded is 2 now!');
        }
        if ($this->Lyw_proofread_article_model->saveAdd($proofread_id, $manager_id, $_SESSION['user_id'], $title, $text, $html, $status, $url, $score)) {
            if ($status == 2) {
                $text_number = $this->lyw_common_model->calculateTextNumber($html, $text);
                $this->lyw_common_model->processJingYan($manager_id, $text_number, $score, $translation_user_id);

                $this->Lyw_article_manager_model->updateProofreadedNumber($manager_id , $text_number);
            }
            exit(json_encode('success'));
        } else {
            exit(json_encode('failure'));
        }
    }

    public function userCenter()
    {
        Permission::zy();
        $data = $this->lyw_common_model->doUserCenter();
        $this->load->view('taskworker_user_center.php', $data);
    }

    public function changeHeaderImg()
    {
        if (isset($_FILES['header_image']) && $_FILES['header_image']['size'] > 0) {
            if (strpos($_FILES['header_image']['type'], 'image') !== false) {
                //有修改头像
                $header_image = $this->lyw_common_model->changeHeaderImg($_FILES['header_image']);
                exit(json_encode('success'));
            } else {
                exit(json_encode('类型错误!'));
            }
        } else {
            exit(json_encode('内容为空!'));
        }
    }

    public function translation()
    {
        Permission::zy();
        $this->load->view('admin_translation', $this->lyw_common_model->readTranslation());
    }

    public function changeOriginal()
    {
        Permission::zy();
        $manager_id = isset($_REQUEST['manager_id']) ? $_REQUEST['manager_id'] : 0;
        if (empty($manager_id)) {
            exit('empty');
        }
        $this->load->model('Lyw_article_manager_model');
        $article_manager_info = $this->Lyw_article_manager_model->getOneListInfoById($manager_id);
        if ($article_manager_info['is_del'] == 1) {
            exit('is del!');
        }
        $this->load->model('Lyw_original_article_model');
        $original_article = $this->Lyw_original_article_model->getOneListInfoById($article_manager_info['original_article_id']);
        $data = array(
            'manager_id' => $manager_id,
            'original_article' => $original_article,
            'article_manager_info' => $article_manager_info,
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
        );
        $this->load->view('taskwork_change', $data);
    }

    public function save()
    {
        Permission::zy(1);
        $text = isset($_POST['text']) ? $_POST['text'] : '';
        $html = isset($_POST['html']) ? $_POST['html'] : '';
        $title = isset($_POST['title']) ? $_POST['title'] : '';
        $manager_id = isset($_POST['manager_id']) ? $_POST['manager_id'] : 0;
        $url = isset($_POST['url']) ? $_POST['url'] : '';
        if (empty($manager_id)) {
            exit('empty');
        }
        $this->load->model('Lyw_article_manager_model');
        $manager_list = $this->Lyw_article_manager_model->getOneListInfoById($manager_id);
        $this->load->model('Lyw_original_article_model');
        sessionOpen();
        if ($this->Lyw_original_article_model->saveAdd($manager_list['original_article_id'], $manager_id, $_SESSION['user_id'], $title, $text, $html, $url)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('failure'));
        }
    }


}